//
//  SecondViewController.m
//  ocdemo
//
//  Created by JIENING ZHANG on 2019/4/5.
//  Copyright © 2019 JIENING ZHANG. All rights reserved.
//

#import "SecondViewController.h"
#import "UIDevice+VGAddition.h"
#import "UIView+Extention.h"
#import "DemoDateTime.h"
#import "UIView+YYClick.h"

@interface SecondViewController ()
@property(nonatomic, strong)UITextView* testInput;

@property(nonatomic, strong)UILabel* testBtn;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    logTSBegin(@"SecondViewController, viewDidLoad");
    
    self.testInput = [[UITextView alloc]initWithFrame:CGRectMake(16, 0, DEF_SCREEN_WIDTH-32, 100)];
    self.testInput.backgroundColor = UIColor.lightGrayColor;
    [self.view addSubview:self.testInput];
    self.testInput.bottom = self.view.height - [UIDevice vg_safeDistanceBottom] - 160;
    
    self.testBtn = [[UILabel alloc]initWithFrame:self.testInput.frame];
    self.testBtn.backgroundColor = UIColor.yellowColor;
    self.testBtn.top = 200;
    [self.view addSubview:self.testBtn];

    @weakify(self)
    [self.testBtn YYViewClick:^(UIView *viewClicked) {
        @strongify(self);if (!self){return;}
        [self.testInput resignFirstResponder];
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidShow:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidHide:)
                                                 name:UIKeyboardDidHideNotification
                                               object:nil];
}

- (void) multiAnimationTest {
    
}


- (void)keyboardWillShow:(NSNotification *)notification {
    logTS(@"keyboardWillShow, notification.userInfo=%@", notification.userInfo);
    
    NSDictionary *userInfo = [notification userInfo];
    CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    // identifying the frame rectangle of the keyboard (in the screen’s coordinate space) after animating the keyboard.
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    if (value) {
        CGRect keyboardRect = [value CGRectValue];
        CGFloat keyboardHeight = keyboardRect.size.height;
        logTS(@"keyboardWillShow, height=%.2f",keyboardRect.size.height);
        [UIView animateWithDuration:duration animations:^{
            self.testInput.bottom = self.view.height-keyboardHeight;
        }];
    } else {
        
    }
    
}


- (void)keyboardDidShow:(NSNotification *)notification {
    logTS(@"keyboardDidShow");
}


- (void)keyboardWillHide:(NSNotification *)notification {
    NSDictionary *userInfo = [notification userInfo];
    logTS(@"keyboardWillHide, notification.userInfo=%@", userInfo);
    //CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
}

-(void)keyboardDidHide:(NSNotification *)notification{
    logTS(@"keyboardDidHide");
}

@end
